<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">发货仓：</label>
                                    <div class="layui-input-inline">
                                        <select name="outWarehouseId" lay-filter="outWarehouseId"
                                                id="outWarehouseNameSelect">
                                            <option value="">按发货仓</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">收货仓：</label>
                                    <div class="layui-input-inline">
                                        <select name="inWarehouseId" lay-filter="inWarehouseId"
                                                id="inWarehouseNameSelect">
                                            <option value="">按收货仓</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">发货人：</label>
                                    <div class="layui-input-inline">
                                        <select name="interest" lay-filter="aihao">
                                            <option value="">按发货人</option>
                                            <option value="0">马云飞</option>
                                            <option value="1">刘总</option>
                                            <option value="2">莫丹</option>
                                            <option value="3">政哥</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">商品名称：</label>
                                    <div class="layui-input-inline">
                                        <input name="productName" class="layui-input" type="text" placeholder="商品名称"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">商品编码：</label>
                                    <div class="layui-input-inline">
                                        <input name="skuCode" class="layui-input" type="text" placeholder="商品编码"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">创建时间：</label>
                                    <div class="layui-input-inline">
                                        <input name="createTime" id="pTime" class="layui-input date-icon" type="text"
                                               placeholder="选择创建时间" autocomplete="off"/>
                                    </div>
                                    <input name="startCreateTime" id="sTime" type="hidden"/>
                                    <input name="endCreateTime" id="eTime" type="hidden"/>
                                </div>
                            </div>
                            <div class="query-rt">
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog"  lay-submit>查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="inventoryOutPlanBtnAdd" class="layui-btn icon-btn color-reseda">添加
                            </button>
                        </div>
                        <div class="lay-btn-rt">
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="inventoryOutPlanTable" lay-filter="inventoryOutPlanTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="inventoryOutPlanTableBar">
    <a class="layui-btn layui-btn layui-btn-xs" lay-event="details">详情</a>
    {{# if(d.status =="confirmed"){ }}
    <!-- <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="audit">审核</a>-->
    {{# } }}
</script>
<!-- 物料信息表格操作列 -->
<script type="text/html" id="deliverProductTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="productDetails">详情</a>
</script>
<!-- 确定出库商品表格操作列 -->
<script type="text/html" id="outProductTableBar">
    {{# if(d.status == "unreceipted"){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="confirmShipments">确定发货</a>
    {{# } }}
</script>


<!-- 表单弹窗 -->
<script type="text/html" id="inventoryOutPlanForm">
    <form lay-filter="inventoryOutPlanForm" id="inventoryOutPlanForm2" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div id="codeSpan" class="layui-form-item" style="display: none">
            <label class="layui-form-label" style="width: 100px;"><span style="color: red">*</span>出库作业单号</label>
            <div class="layui-form-mid layui-word-aux">
                <span style="color: red" id="code">P20220506001</span>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px;"><span style="color: red">*</span>发货商</label>
            <div class="layui-form-mid layui-word-aux">
                <span style="color: red" id="enterpriseName"></span>
            </div>
        </div>
        <div id="storagePlanSelect" class="layui-form-item">
            <label class="layui-form-label" style="width: 100px;margin-left: 2px;">
                <span style="color: red">*</span>所属出库计划
            </label>
            <div class="layui-input-block" style="width: 200px;margin-left: 132px;">
                <select name="planId" id="planCode" lay-filter="planCode" required lay-verify="required"
                        placeholder="请选择所属出库计划">
                    <option value="">请选择</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px;"><span style="color: red">*</span>出库类型</label>
            <div class="layui-form-mid layui-word-aux">
                <span style="color: red" id="inoutType"></span>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px;"><span style="color: red">*</span>出库计划备注</label>
            <div class="layui-form-mid layui-word-aux">
                <span style="color: red" id="remark"></span>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px;">出货仓库</label>
            <div class="layui-form-mid layui-word-aux">
                <span style="color: red" id="outWarehouseName"></span>
            </div>
        </div>
        <!-- 运输方式:logistics(0,快递运输);express(1,快递运输);self(2,自行运输)-->
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px;padding-left: 16px;"><span
                    style="color: red">*</span>运输方式</label>
            <div class="layui-input-block" style="width: 200px;margin-left: 132px;">
                <select name="transportType" required lay-verify="required" lay-filter="transportType"
                        placeholder="请选择运输方式">
                    <option value="">运输方式</option>
                    <option value="logistics">物流运输</option>
                    <option value="express">快递运输</option>
                    <option value="self">自行运输</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item" id="companyIdSelect" style="display: none">
            <label class="layui-form-label" style="width: 100px;padding-left: 16px;">物流企业</label>
            <div class="layui-input-block" style="width: 200px;margin-left: 132px;">
                <select name="companyId" placeholder="请选择物流企业" id="companyIdOption">
                    <option value="">选择物流企业</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item" id="expressCodeInput" style="display: none">
            <label class="layui-form-label" style="width: 100px">物流单号</label>
            <div class="layui-input-block" style="width: 200px;margin-left: 132px;">
                <input type="text" name="expressCode" placeholder="请输入物流单号" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" id="carNoInput" style="display: none">
            <label class="layui-form-label" style="width: 100px">车牌号</label>
            <div class="layui-input-block" style="width: 200px;margin-left: 132px;">
                <input type="text" name="carNo" placeholder="请输入车牌号" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" id="customerConcatInput" style="display: none">
            <label class="layui-form-label" style="width: 100px">联系人</label>
            <div class="layui-input-block" style="width: 200px;margin-left: 132px;">
                <input type="text" name="customerConcat" placeholder="请输入联系人" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" id="customerMobleInput" style="display: none">
            <label class="layui-form-label" style="width: 100px">电话</label>
            <div class="layui-input-block" style="width: 200px;margin-left: 132px;">
                <input type="text" name="customerMoble" placeholder="请输入电话" autocomplete="off"
                       class="layui-input">
            </div>
        </div>

        <!--<div class="layui-form-item ">
            <label class="layui-form-label" style="width: 100px;padding-left: 16px;"><span style="color: red">*</span>选择目标仓库</label>
            <div class="layui-input-inline">
                <select  lay-filter="province" lay-search lay-verify="required">
                    <option value>请选择省份</option>
                </select>
            </div>
            <div class="layui-input-inline">
                <select name="city" lay-filter="city" lay-search lay-verify="required">
                    <option value>请选择市区</option>
                </select>
            </div>
            <div class="layui-input-inline">
                <select name="area" lay-filter="area" lay-search lay-verify="required">
                    <option value>请选择区县</option>
                </select>
            </div>
        </div>-->
        <div id="outWarehouseIdSelect" class="layui-form-item">
            <label class="layui-form-label" style="width: 100px;padding-left: 16px;"><span
                    style="color: red">*</span>选择目标仓库</label>
            <div class="layui-input-block" style="width: 200px;margin-left: 132px;">
                <select name="inWarehouseId" id="outWarehouseId" required lay-verify="required" placeholder="请选择目标仓库">
                    <option value="">目标仓库</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px;">发货商品信息</label>
        </div>
        <table class="layui-table" id="deliverProductTable" lay-filter="deliverProductTable"></table>
        <div class="layui-form-item" id="deliverProductBtn">
            <label class="layui-form-label" style="width: 100px;"></label>
            <div class="layui-form-mid layui-word-aux">
                <button id="deliveryProductBtnAdd" type="button" class="layui-btn icon-btn"><i
                        class="layui-icon">&#xe654;</i>添加出库商品
                </button>
            </div>
        </div>
        <div id="remarkInput" class="layui-form-item">
            <label class="layui-form-label" style="width: 100px">出库作业描述</label>
            <div class="layui-input-block" style="width: 200px;margin-left: 132px;">
                <input type="text" name="remark" placeholder="请输入描述" autocomplete="off"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item text-left" style="margin-left: 134px;">
            <button class="layui-btn" lay-filter="inventoryOutPlanFormSubmit" lay-submit>提交</button>
        </div>
    </form>
</script>

<!-- 出库作业记录详情弹窗 -->
<script type="text/html" id="outWorkDetailTableForm">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <table class="layui-table" id="outWorkDetailsTable" lay-filter="outWorkDetailsTable"></table>
            </div>
        </div>
    </div>
</script>
<!-- 出库作业记录详情操作栏 -->
<script type="text/html" id="outWorkDetailsTableBar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="particulars">明细</a>
</script>
<!-- 出库物料明细详情弹窗 -->
<script type="text/html" id="outProductParticularsTableForm">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <table class="layui-table" id="outProductParticularsTable"
                       lay-filter="outProductParticularsTable"></table>
                <table class="layui-table" id="outProductParticularsTable2"
                       lay-filter="outProductParticularsTable2"></table>
            </div>
        </div>
    </div>
</script>

<!-- 添加出库作业签收物料弹窗 -->
<script type="text/html" id="addOutProductForm">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <form class="layui-form" action="" lay-filter="addOutProductForm">
                    <div class="layui-form-item" style="margin-top: 20px;">
                        <label class="layui-form-label">扫描包装码</label>
                        <div class="layui-inline">
                            <input name="packCode" class="layui-input" type="text" placeholder="请输包装码编号"/>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn" type="button" id="addProductSingSearch" lay-submit
                                    lay-filter="addProductSingSearch">添加签收信息
                            </button>
                        </div>
                    </div>

                </form>
                <table class="layui-table" id="outProductTable" lay-filter="outProductTable"></table>
                <div class="layui-form-item" style="margin-top: 20px;text-align: center;">
                    <div class="layui-inline">
                        <button id="productConfirmBtn" type="button" class="layui-btn icon-btn">确定提交</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- 表格 -->
</script>

<!-- 发货商品信息详情弹窗 -->
<script type="text/html" id="outProductDetailsTableForm">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <table class="layui-table" id="outProductDetailsTable" lay-filter="outProductDetailsTable"></table>
                <table class="layui-table" id="outProductDetailsTable2" lay-filter="outProductDetailsTable2"></table>
            </div>
        </div>
    </div>
    <!-- 表格 -->
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var laydate = layui.laydate;
        var type = 0; //0是添加 1是详情
        var productPackLabeList = [];//物料包装条码数据
        var productSkuCodeArr = [];
        var deliverProductList = [];//出库物料数据
        var packLabelSkuCodeList = [];//扫码包装码商品的id
        var outData = null;

        //时间范围
        laydate.render({
            elem: 'input[name="createTime"]',
            type: 'date',
            range: true,
            done: function (value, date, endDate) {
                var dateValue = value.split(" - ");
                $("input[name='startCreateTime']").val(dateValue[0] + " 00:00:00");
                $("input[name='endCreateTime']").val(dateValue[1] + " 00:00:00");
            }
        });

        // option渲染
        function loadSelectHtml(data, tips, htmltarget, type) {
            let html = ' <option value>' + tips + '</option>';
            if (!data || data == null) {
                return;
            }
            for (let i = 0; i < data.length; i++) {
                if (type == 1) {
                    html += `<option value='${data[i].productType}'>${data[i].name}</option>`;
                } else if (type == 2) {
                    html += `<option value='${data[i].id}'>${data[i].name}</option>`;
                } else if (type == 3) {
                    html += `<option value='${data[i].id}'>${data[i].warehouseName}</option>`;
                } else if (type == 4) {
                    html += `<option value='${data[i].skuCode}'  skuCode='${data[i].skuCode}'>${data[i].skuName}</option>`;
                } else if (type == 5) {
                    html += `<option value='${data[i].skuCode}'>${data[i].skuName}</option>`;
                }
            }

            $("form select[name='" + htmltarget + "']").html(html);
            form.render('select');
        }

        //查询仓库
        admin.req('enterprisecab/warehouse/admin/warehouse/getWarehouseVOAll', {}, function (res) {
            if (res.code == 200) {
                if (res.data.length > 0) {
                    $.each(res.data, function (index, item) {
                        $("#outWarehouseNameSelect").append(new Option(item.name, item.id));
                    })
                    $.each(res.data, function (index, item) {
                        $("#inWarehouseNameSelect").append(new Option(item.name, item.id));
                    })
                    form.render('select');
                }
            } else {
                layer.msg(res.msg, {icon: 2});
            }
        }, 'GET');
        // 渲染表格
        var insTb = table.render({
            elem: '#inventoryOutPlanTable',
            url: config.base + 'enterprisecab/order/admin/inventoryOutWorkRecord/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'}
                , {field: 'code', sort: true, title: '出库计划单编号', width: 150}
                , {field: 'planCode', sort: true, title: '所属出库计划', width: 150}
                , {field: 'outWarehouseName', sort: true, title: '发货仓库'}
                , {field: 'consigner', sort: true, title: '发货人'}
                , {field: 'quantityTotal', sort: true, title: '出库商品数量', width: 150}
                // ,{field: 'supplyAmount', sort: true, title: '出库商品单价', width: 150}
                , {field: 'marketAmount', sort: true, title: '市场总价', width: 160}
                , {field: 'supplyAmount', sort: true, title: '总供货金额'}
                , {field: 'inWarehouseName', sort: true, title: '入库云仓'}
                //,{field: 'no', sort: true, title: '联系人'}
                , {field: 'areaName', sort: true, title: '仓库所在地'}
                //,{field: 'statusDesc', sort: true, title: '状态'}
                , {field: 'createTime', sort: true, title: '添加时间', width: 160}
                , {align: 'center', toolbar: '#inventoryOutPlanTableBar', title: '操作', width: 200}
            ]],
            done: function (res, curr, count) {
                //移除按钮
                var menuButton = config.getMenuButton(location.hash);
                if (menuButton.indexOf("搜索") == -1) {
                    $('.layui-form.toolbar').remove();
                }
                if (menuButton.indexOf("修改") == -1) {
                    $("[lay-event='edit']").remove();
                }
                if (menuButton.indexOf("删除") == -1) {
                    $("[lay-event='del']").remove();
                }
            }
        });
        form.render('select');
        // 工具条点击事件
        table.on('tool(inventoryOutPlanTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'details') { //详情
                showOutWorkDetailModel(data);
            } else if (obj.event === 'audit') { // 审核
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(inventoryOutPlanTable)', function (obj) {
            table.reload('inventoryOutPlanTable', {
                initSort: obj
                , where: {
                    sort: obj.field
                    , order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(inventoryOutPlanSearch)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}}, 'data');
            return false;
        });

        // 添加按钮点击事件
        $('#inventoryOutPlanBtnAdd').click(function () {
            type = 0;
            outData = null;
            deliverProductList = [];//每次打开弹框清空出库物料数据
            showEditModel(type, outData);
        });

        // 显示编辑弹窗
        function showEditModel(type, data) {
            admin.open({
                type: 1,
                area: ['1400px', '800px'],
                offset: '65px',
                title: data ? '修改云仓商品出库记录' : '添加云仓商品出库记录',
                content: $('#inventoryOutPlanForm').html(),
                success: function () {
                    //查询企业
                    admin.req('enterprisecab/common/admin/cabCommon/getEnterpriseName', {}, function (res) {
                        if (res.code == 200) {
                            $("#enterpriseName").html(res.msg);
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'GET');
                    deliverProductList = [];
                    productPackLabeList = [];
                    //查询出库计划的物料数据
                    admin.req('enterprisecab/order/admin/inventoryOutPlan/getPlanAll', {}, function (res) {
                        if (res.code == 200) {
                            if (res.data.length > 0) {
                                $.each(res.data, function (index, item) {
                                    $("#planCode").append(new Option(item.code, item.id));
                                })
                                form.render('select');
                            }
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'GET');

                    //监听出库计划select
                    form.on('select(planCode)', function (data) {
                        admin.req('enterprisecab/order/admin/inventoryOutWorkRecord/getInventoryOutPlanById', {id: data.value}, function (res) {
                            if (res.code == 200) {
                                console.log(res.data)
                                $("#outWarehouseName").html(res.data.outWarehouseName);
                                $("#remark").html(res.data.remark);
                                $("#inoutType").html(res.data.inoutType);
                                //出库类型等于1 就是调拨到云仓
                                if (res.data.inoutType == "调拨到云仓入库") {
                                    let outWarehouseNameArr = [];
                                    $("#outWarehouseId").html("");//每次先清空
                                    //出库类型是调拨到云仓的话需要查询平台端的仓库数据 其他类型则是查询企业自有的仓库
                                    admin.req('enterprisecab/order/admin/inventoryOutPlan/getPlatformWarehouseAll', {}, function (waRes) {
                                        if (waRes.code == 200) {
                                            if (waRes.data.length > 0) {
                                                outWarehouseNameArr = waRes.data;
                                                /*$.each(outWarehouseNameArr,function (index,item) {
                                                    $("#outWarehouseId").append(new Option(item.name,item.id));
                                                })
                                                $("#outWarehouseId").val(res.data.outWarehouseId);
                                                form.render('select');*/

                                                loadSelectHtml(outWarehouseNameArr, "选择目标仓库", "inWarehouseId", 2);
                                            }
                                        } else {
                                            layer.msg(res.msg, {icon: 2});
                                        }
                                    }, 'GET');
                                } else {
                                    //查询企业自有仓库
                                    admin.req('enterprisecab/warehouse/admin/warehouse/getWarehouseVOAll', {}, function (res) {
                                        if (res.code == 200) {
                                            if (res.data.length > 0) {
                                                /*$.each(res.data,function (index,item) {
                                                    $("#outWarehouseId").append(new Option(item.name,item.id));
                                                })
                                                form.render('select');*/
                                                loadSelectHtml(res.data, "选择目标仓库", "inWarehouseId", 2);
                                            }
                                        } else {
                                            layer.msg(res.msg, {icon: 2});
                                        }
                                    }, 'GET');
                                }
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GET');
                        //出货商品详情
                        admin.req('enterprisecab/order/admin/inventoryOutPlan/getInventoryOutPlanDetailVOList', {planId: data.value}, function (res) {
                            if (res.code == 200) {
                                deliverProductList = res.data;
                                for (let i = 0; i < deliverProductList.length; i++) {
                                    deliverProductList[i].quantityOut = 0;
                                    deliverProductList[i].packLabeCode = "";//用于标识确认发货的包装码数据
                                }
                                /*table.reload('deliverProductTable',{
                                    data: deliverProductList,
                                })*/
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GET');
                    });
                    //监听运输方式select
                    form.on('select(transportType)', function (data) {
                        if (data.value == "logistics" || data.value == "express") {
                            $("#companyIdSelect").show();
                            $("#expressCodeInput").show();
                            $("#customerConcatInput").hide();
                            $("#carNoInput").hide();
                            $("#customerMobleInput").hide();
                            //查询物流公司
                            admin.req('enterprise/invoicing/admin/transportCompany/getAll', {}, function (res) {
                                if (res.code == 200) {
                                    if (res.data.length > 0) {
                                        $.each(res.data, function (index, item) {
                                            $("#companyIdOption").append(new Option(item.name, item.id));
                                        })
                                        form.render('select');
                                    }
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }, 'GET');
                        } else if (data.value == "self") {
                            $("#customerConcatInput").show();
                            $("#carNoInput").show();
                            $("#customerMobleInput").show();
                            $("#companyIdSelect").hide();
                            $("#expressCodeInput").hide();
                        }
                    });
                    //发货时间
                    laydate.render({
                        elem: '#sendTime'
                        , type: 'datetime'
                    });
                    //添加出库商品点击事件
                    $("#deliveryProductBtnAdd").click(function () {
                        showProductModel();
                    })
                    var deliverProductTable = table.render({
                        elem: '#deliverProductTable',
                        data: deliverProductList,
                        cellMinWidth: 100,
                        cols: [[
                            {field: 'number', title: '序号', width: 60, type: 'numbers'}
                            , {field: 'id', title: 'ID', width: 60, hide: true}
                            , {field: 'skuCode', sort: true, title: '物料编号', width: 150}
                            , {
                                field: 'skuPic', sort: true, title: '图片', width: 100
                                , templet: '<div><img  src="{{ d.skuPic }}"></div>'
                            }
                            , {field: 'productName', sort: true, title: '物料名称', width: 150}
                            //, {field: 'skuSpecValueNames', title: 'SKU规格'}
                            , {field: 'skuSpecCode', title: '规格', width: 150}
                            , {field: 'enterpriceCode', title: '企业货号'}
                            , {field: 'barcode', title: '国标条码'}
                            , {field: 'unitName', title: '最小单位'}
                            , {field: 'supplyPrice', title: '平台结算价'}
                            , {field: 'quantityOut', title: '出库数量', width: 120}
                            , {field: 'quantity', title: '计划出库数', width: 120}
                            , {field: 'totalMoney', title: '总金额'}
                            , {align: 'center', toolbar: '#deliverProductTableBar', title: '操作'}
                        ]]
                        , done: function (res, curr, count) {

                        }
                    });
                    // 工具条点击事件
                    table.on('tool(deliverProductTable)', function (obj) {
                        var data = obj.data;
                        console.log(data);
                        if (obj.event === 'productDetails') { //发货商品详情
                            showOutProductDetailModel(data);

                        }
                    });
                    // 表单提交事件
                    form.on('submit(inventoryOutPlanFormSubmit)', function (d) {
                        if (packLabelSkuCodeList.length == 0) {
                            layer.msg("请先添加出库商品", {icon: 2});
                            return false;
                        }
                        var quantityOut = 0;
                        for (let i = 0; i < deliverProductList.length; i++) {
                            quantityOut = quantityOut + deliverProductList[i].quantityOut;
                        }

                        console.log("物料数据", deliverProductList)

                        d.field.quantityOut = quantityOut;
                        d.field.productJson = JSON.stringify(deliverProductList.filter(item => item.quantityOut != 0));
                        d.field.productPackLabeJson = JSON.stringify(productPackLabeList.filter(item => item.status != "unreceipted"));
                        console.log("包装码数据", d.field.productPackLabeJson)
                        layer.load(2);
                        admin.req('enterprisecab/order/admin/inventoryOutWorkRecord/addOutInventoryInoutJob', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('inventoryOutPlanTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'POST');
                        return false;
                    });
                }
            });
        }

        //出库作业记录详情弹框
        function showOutWorkDetailModel(data) {
            var index = admin.open({
                type: 1,
                area: ['1400px', '800px'],
                offset: '65px',
                title: '云仓出库记录详情',
                content: $('#outWorkDetailTableForm').html(),
                success: function () {
                    var outWorkDetailsList = [];
                    admin.req('enterprisecab/order/admin/inventoryOutWorkRecord/getOutWorkProductDetail', {jobId: data.id}, function (res) {
                        if (res.code == 200) {
                            outWorkDetailsList = res.data;
                            table.reload('outWorkDetailsTable', {
                                data: outWorkDetailsList,
                            })
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'GET');

                    var outWorkDetailsTable = table.render({
                        elem: '#outWorkDetailsTable',
                        data: outWorkDetailsList,
                        cellMinWidth: 100,
                        cols: [[
                            {field: 'number', title: '序号', width: 60, type: 'numbers'}
                            , {field: 'id', title: 'ID', width: 60, hide: true}
                            , {field: 'skuCode', sort: true, title: '物料编号', width: 150}
                            , {
                                field: 'skuPic', sort: true, title: '图片', width: 100
                                , templet: '<div><img  src="{{ d.skuPic }}"></div>'
                            }
                            , {field: 'productName', sort: true, title: '物料名称', width: 150}
                            //, {field: 'skuSpecValueNames', title: 'SKU规格'}
                            , {field: 'skuSpecCode', title: '规格', width: 150}
                            , {field: 'enterpriceCode', title: '企业货号'}
                            , {field: 'barcode', title: '国标条码'}
                            , {field: 'total', title: '平台结算价'}
                            , {field: 'quantity', title: '出库数量', width: 120}
                            , {field: 'unitName', title: '单位'}
                            , {field: 'totalMoney', title: '总金额'}
                            , {field: 'planQuantity', title: '待录入数', width: 120, style: 'color:red'}
                            , {align: 'center', toolbar: '#outWorkDetailsTableBar', title: '操作', width: 100}
                        ]]
                        , done: function (res, curr, count) {

                        }
                    });
                    // 工具条点击事件
                    table.on('tool(outWorkDetailsTable)', function (obj) {
                        var data = obj.data;
                        if (obj.event === 'particulars') { //明细
                            showOutProductParticularsModel(data);
                        }
                    });

                }
            });
        }

        //出库物料明细详情弹窗
        function showOutProductParticularsModel(data) {
            var index = admin.open({
                type: 1,
                area: ['1400px', '800px'],
                offset: '65px',
                title: ' 出库物料明细详情',
                content: $('#outProductParticularsTableForm').html(),
                success: function () {
                    var productParticularsList = [];
                    productParticularsList.push(data);
                    var outProductParticularsTable = table.render({
                        elem: '#outProductParticularsTable',
                        data: productParticularsList,
                        cellMinWidth: 100,
                        cols: [[
                            {field: 'number', title: '序号', width: 60, type: 'numbers'}
                            , {field: 'id', title: 'ID', width: 60, hide: true}
                            , {field: 'skuCode', sort: true, title: '物料编号', width: 150}
                            , {
                                field: 'skuPic', sort: true, title: '图片', width: 100
                                , templet: '<div><img  src="{{ d.skuPic }}"></div>'
                            }
                            , {field: 'productName', sort: true, title: '物料名称', width: 150}
                            //, {field: 'skuSpecValueNames', title: 'SKU规格'}
                            , {field: 'skuSpecCode', title: '规格', width: 150}
                            , {field: 'enterpriceCode', title: '企业货号'}
                            , {field: 'barcode', title: '国标条码'}
                            , {field: 'total', title: '平台结算价'}
                            , {field: 'quantity', title: '出库数量', width: 120}
                            , {field: 'unitName', title: '单位'}
                            , {field: 'totalMoney', title: '总金额'}
                            , {field: 'planQuantity', title: '待录入数', width: 120, style: 'color:red'}
                        ]]
                        , done: function (res, curr, count) {

                        }
                    });

                    var outWorkPackList = [];
                    admin.req('enterprisecab/order/admin/inventoryOutWorkRecord/getOutWorkPackList', {
                        jobId: data.id,
                        jobProductId: data.jobProductId
                    }, function (res) {
                        if (res.code == 200) {
                            outWorkPackList = res.data;
                            table.reload('outProductParticularsTable2', {
                                data: outWorkPackList,
                            })
                            console.log(outWorkPackList)
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'GET');
                    var outProductParticularsTable2 = table.render({
                        elem: '#outProductParticularsTable2',
                        data: outWorkPackList,
                        cellMinWidth: 100,
                        cols: [[
                            {field: 'number', title: '序号', width: 60, type: 'numbers'}
                            , {field: 'id', title: 'ID', width: 60, hide: true}
                            , {field: 'packLabeCode', sort: true, title: '包装编码'}
                            , {field: 'productName', sort: true, title: '物料名称'}
                            , {field: 'packName', title: '包装规格'}
                            //, {field: 'skuCode', title: 'skuCode'}
                            , {field: 'unitName', title: '最小单位'}
                            , {field: 'packLabeQuantity', title: '单位包装数'}
                            , {field: 'quantity', title: '发货数', style: 'color:red'}
                            //, {align: 'center', toolbar: '#outProductTableBar', title: '操作', width: 150}//,hide : type == 0 ? true : false
                        ]]
                    });
                }
            });
        }

        function showProductModel() {
            var packLabeIndex = admin.open({
                type: 1,
                area: ['1400px', '800px'],
                offset: '65px',
                title: '添加出库商品信息',
                content: $('#addOutProductForm').html(),
                success: function () {
                    form.render('select');
                    form.on('submit(addProductSingSearch)', function (obj) {
                        if (obj.field.packCode == "") {
                            layer.msg("请输入包装条码", {icon: 2});
                            return;
                        }
                        //如果productPackLabeList数组中的对象item.code的值与packCode相同，会返回一个大于0的数
                        if (productPackLabeList.length > 0) {
                            for (let i = 0; i < productPackLabeList.length; i++) {
                                if (obj.field.packCode == productPackLabeList[i].code) {
                                    layer.msg("包装条码已存在当前列表", {icon: 2});
                                    return false;
                                }
                            }
                        }
                        /*let index = productPackLabeList.findIndex((item) => item.code === obj.field.packCode);
                            if(index > 0){
                                layer.msg("包装条码已存在当前列表", {icon: 2});
                                return false;
                            }*/
                        admin.req('enterprise/product/admin/productPackLabel/getProductPackLabelVOList', {
                            code: obj.field.packCode,
                            type: 1
                        }, function (res) {
                            if (res.code == 200) {
                                let packLabeList = res.data;
                                packLabeList.quantityOut = packLabeList.surplusQuantity;
                                packLabeList.status = "unreceipted";//状态 默认没有确定发货
                                //将出库计划数赋值给剩余计划数
                                for (let j = 0; j < deliverProductList.length; j++) {
                                    //判断物料包装码数据的物料skuCode是否等于出货商品信息的skuCode
                                    if (packLabeList.skuCode == deliverProductList[j].skuCode) {
                                        packLabeList.planQuantity = deliverProductList[j].quantity;
                                    }
                                }
                                productPackLabeList.push(packLabeList);
                                console.log("包装条码数据", productPackLabeList)
                                table.reload('outProductTable', {
                                    data: productPackLabeList,
                                })
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GET');
                        return false;
                    });
                    if (packLabelSkuCodeList.length > 0) {
                        for (let i = 0; i < productPackLabeList.length; i++) {
                            for (let j = 0; j < deliverProductList.length; j++) {
                                //判断两个数组的包装码code是否相同  相同将确认发货商品的出库赋值给物料包装码的发货数
                                if (productPackLabeList[i].code == deliverProductList[j].packLabeCode) {
                                    productPackLabeList[i].quantityOut = deliverProductList[j].quantityOut;
                                    productPackLabeList[i].status = "sign";
                                }
                            }
                        }
                        table.reload('outProductTable', {
                            data: productPackLabeList,
                        })
                    }
                    /*admin.req('enterprise/product/admin/productPackLabel/getProductPackLabelVOList', {}, function (res) {
                        if (res.code == 200) {
                            productPackLabeList = res.data;
                            for (let i = 0; i < productPackLabeList.length; i++) {
                                productPackLabeList[i].quantityOut = productPackLabeList[i].surplusQuantity;
                                productPackLabeList[i].status = "unreceipted";//状态 默认没有确定发货
                                //将出库计划数赋值给剩余计划数
                                for (let j = 0; j <deliverProductList.length; j++) {
                                    //判断物料包装码数据的物料skuCode是否等于出货商品信息的skuCode
                                    if(productPackLabeList[i].skuCode == deliverProductList[j].skuCode){
                                        productPackLabeList[i].planQuantity = deliverProductList[j].quantity;
                                    }
                                    //判断两个数组的包装码code是否相同  相同将确认发货商品的出库赋值给物料包装码的发货数
                                    if(productPackLabeList[i].code == deliverProductList[j].packLabeCode){
                                        productPackLabeList[i].quantityOut = deliverProductList[j].quantityOut;
                                        productPackLabeList[i].status = "sign";
                                    }
                                }
                                //已经确认发货的 将状态改为确认
                                /!*if(packLabelSkuCodeList.length > 0){
                                    for (let i = 0; i < packLabelSkuCodeList.length; i++) {
                                        if(packLabelSkuCodeList[i] == productPackLabeList[i].id){
                                            productPackLabeList[i].status = "sign";
                                        }
                                    }
                                }*!/
                            }


                            table.reload('outProductTable',{
                                data: productPackLabeList,
                            })
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'GET');*/
                    var productTable = table.render({
                        elem: '#outProductTable',
                        data: productPackLabeList,
                        cellMinWidth: 100,
                        cols: [[
                            {field: 'number', title: '序号', width: 60, type: 'numbers'}
                            , {field: 'id', title: 'ID', width: 60, hide: true}
                            , {field: 'status', title: 'status', width: 60, hide: true}
                            , {field: 'code', sort: true, title: '包装编码', width: 150}
                            , {field: 'productName', sort: true, title: '物料名称', width: 120}
                            , {field: 'packName', title: '包装规格'}
                            //, {field: 'skuCode', title: 'skuCode'}
                            , {field: 'quantity', title: '单位包装数'}
                            , {field: 'unitName', title: '最小单位'}
                            , {field: 'planQuantity', title: '剩余计划发货数'}
                            , {field: 'surplusQuantity', title: '包装余数'}
                            , {field: 'quantityOut', title: '发货数(可填写)'}
                            , {align: 'center', toolbar: '#outProductTableBar', title: '操作', width: 150}//,hide : type == 0 ? true : false
                        ]]

                        , done: function (res, curr, count) {
                            let tableView = this.elem.next();
                            layui.each(res.data, function (i, item) {
                                if (item.status === "unreceipted") {
                                    tableView.find('tr[data-index=' + i + ']').find('td').eq(10).data('edit', true); //设置下标为9的单元格为可编辑
                                }
                            })

                        }
                    });
                    //编辑单元格事件
                    table.on('edit(outProductTable)', function (obj) { //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
                        console.log(obj.value); //得到修改后的值
                        //console.log(obj.field); //当前编辑的字段名
                        console.log(obj.data); //所在行的所有相关数据
                        if (parseInt(obj.value) > parseInt(obj.data.surplusQuantity)) {
                            layer.msg("不能大于包装余数", {icon: 2});
                            table.reload('outProductTable', {
                                data: productPackLabeList,
                            })
                            return false;
                        }
                        if (parseInt(obj.value) > parseInt(obj.data.planQuantity)) {
                            layer.msg("不能大于出库计划数", {icon: 2});
                            table.reload('outProductTable', {
                                data: productPackLabeList,
                            })
                            return false;
                        }
                        const row = productPackLabeList.find(x => x.id === obj.data.id);
                        if (row) {
                            row.quantityOut = obj.value;
                            table.reload('outProductTable', {
                                data: productPackLabeList,
                            })
                        }
                        layer.msg("修改成功");
                    });
                    // 工具条点击事件
                    table.on('tool(outProductTable)', function (obj) {
                        var data = obj.data;
                        console.log(data.skuCode);
                        if (obj.event === 'confirmShipments') { //确定发货
                            const row = deliverProductList.find(x => x.skuCode === obj.data.skuCode);
                            if (!row) {
                                layer.msg("此物料不在出库计划里面，不能确定发货", {icon: 2});
                            } else {
                                if (parseInt(data.quantityOut) > parseInt(obj.data.surplusQuantity)) {
                                    layer.msg("不能大于包装余数", {icon: 2});
                                    table.reload('outProductTable', {
                                        data: productPackLabeList,
                                    })
                                    return false;
                                }
                                if (parseInt(data.quantityOut) > parseInt(obj.data.planQuantity)) {
                                    layer.msg("不能大于出库计划数", {icon: 2});
                                    table.reload('outProductTable', {
                                        data: productPackLabeList,
                                    })
                                    return false;
                                }
                                for (let i = 0; i < productPackLabeList.length; i++) {
                                    if (productPackLabeList[i].id == data.id) {
                                        productPackLabeList[i].status = "sign";
                                    }
                                }
                                table.reload('outProductTable', {
                                    data: productPackLabeList,
                                })
                                packLabelSkuCodeList.push(data.id);
                                console.log(packLabelSkuCodeList);
                            }

                        } else if (obj.event === 'updateSign') { // 修改

                        }
                    });
                    // 搜索
                    form.on('submit(productSearch)', function (data) {
                        productTable.reload({where: data.field, page: {curr: 1}}, 'data');
                        return false;
                    });
                    //确定提交
                    $("#productConfirmBtn").click(function () {
                        if (packLabelSkuCodeList.length == 0) {
                            layer.msg("请先确定发货", {icon: 2});
                            return false;
                        }
                        //每次先清空
                        for (let j = 0; j < deliverProductList.length; j++) {
                            deliverProductList[j].quantityOut = 0;
                        }
                        //循环包装码表格数据
                        for (let i = 0; i < productPackLabeList.length; i++) {
                            //判断是否确定发货
                            if (productPackLabeList[i].status == "sign") {
                                //将确定发货的发货数赋值给发货商品信息的出库数量
                                for (let j = 0; j < deliverProductList.length; j++) {
                                    if (productPackLabeList[i].skuCode == deliverProductList[j].skuCode) {
                                        deliverProductList[j].packLabeCode = productPackLabeList[i].code;
                                        deliverProductList[j].quantityOut = parseInt(deliverProductList[j].quantityOut) + parseInt(productPackLabeList[i].quantityOut);
                                    }
                                }
                            }
                        }
                        console.log("确认发货的数据", deliverProductList)
                        layer.close(packLabeIndex);//关闭最新打开的窗口
                        //确认发货的物料信息，重载deliverProductTable
                        table.reload('deliverProductTable', {
                            //出库数大于0的出库物料才显示
                            data: deliverProductList.filter(item => item.quantityOut != 0),
                            //data: deliverProductList
                        })
                    })

                }
            });
        }


        function showOutProductDetailModel(data) {
            console.log("---------", data.skuCode);
            var packLabeIndex = admin.open({
                type: 1,
                area: ['1400px', '800px'],
                offset: '65px',
                title: '出库物料信息详情',
                content: $('#outProductDetailsTableForm').html(),
                success: function () {
                    var outProductDetailsList = [];
                    outProductDetailsList.push(data);
                    var outProductDetailsTable = table.render({
                        elem: '#outProductDetailsTable',
                        data: outProductDetailsList,
                        cellMinWidth: 100,
                        cols: [[
                            {field: 'number', title: '序号', width: 60, type: 'numbers'}
                            , {field: 'id', title: 'ID', width: 60, hide: true}
                            , {field: 'skuCode', sort: true, title: '物料编号', width: 150}
                            , {
                                field: 'skuPic', sort: true, title: '图片', width: 100
                                , templet: '<div><img  src="{{ d.skuPic }}"></div>'
                            }
                            , {field: 'productName', sort: true, title: '物料名称', width: 150}
                            //, {field: 'skuSpecValueNames', title: 'SKU规格'}
                            , {field: 'skuSpecCode', title: '规格', width: 150}
                            , {field: 'enterpriceCode', title: '企业货号'}
                            , {field: 'barcode', title: '国标条码'}
                            , {field: 'unitName', title: '最小单位'}
                            , {field: 'supplyPrice', title: '平台结算价'}
                            , {field: 'quantityOut', title: '出库数量', width: 120}
                            , {field: 'quantity', title: '待录入数', width: 120, style: 'color:red'}
                            , {field: 'totalMoney', title: '总金额'}
                        ]]
                        , done: function (res, curr, count) {

                        }
                    });
                    var outProductDetailsList2 = [];
                    for (let i = 0; i < productPackLabeList.length; i++) {
                        if (productPackLabeList[i].skuCode == data.skuCode && productPackLabeList[i].status == "sign") {
                            outProductDetailsList2.push(productPackLabeList[i]);
                        }
                    }
                    var outProductDetailsTable2 = table.render({
                        elem: '#outProductDetailsTable2',
                        data: outProductDetailsList2,
                        cellMinWidth: 100,
                        cols: [[
                            {field: 'number', title: '序号', width: 60, type: 'numbers'}
                            , {field: 'id', title: 'ID', width: 60, hide: true}
                            , {field: 'status', title: 'status', width: 60, hide: true}
                            , {field: 'code', sort: true, title: '包装编码', width: 150}
                            , {field: 'productName', sort: true, title: '物料名称', width: 120}
                            , {field: 'pageName', title: '包装规格'}
                            , {field: 'skuCode', title: 'skuCode'}
                            , {field: 'quantity', title: '单位包装数'}
                            , {field: 'unitName', title: '最小单位'}
                            , {field: 'surplusQuantity', title: '包装余数'}
                            , {field: 'quantityOut', title: '发货数'}
                            //, {align: 'center', toolbar: '#outProductTableBar', title: '操作', width: 150}//,hide : type == 0 ? true : false
                        ]]
                        /*,done: function(res, curr, count){
                            let tableView = this.elem.next();
                            layui.each(res.data, function (i, item) {
                                if (item.status === "unreceipted") {
                                    tableView.find('tr[data-index=' + i + ']').find('td').eq(10).data('edit', true); //设置下标为9的单元格为可编辑
                                }
                            })
                        }*/
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要审核吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprisecab/order/admin/inventoryOutWorkRecord/updateStatus', {id: obj.data.id}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        table.reload('inventoryOutPlanTable', {})
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'PUT');
            });
        }

        // 导出excel
        $('#inventoryOutPlanBtnExport').click(function () {
            var checkRows = table.checkStatus('inventoryOutPlanTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>